package com.suishoupai.xmb.service;


import com.suishoupai.xmb.dao.CutGoodsMapper;
import com.suishoupai.xmb.dao.CutSearchMapper;
import com.suishoupai.xmb.dao.GoodsMapper;
import com.suishoupai.xmb.dao.SearchMapper;
import com.suishoupai.xmb.entity.CutGoods;
import com.suishoupai.xmb.entity.Goods;
import com.suishoupai.xmb.entity.Search;
import com.suishoupai.xmb.entity.SearchTypeEnum;
import com.suishoupai.xmb.utils.ParticipleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * Created by caojian on 2017/3/10.
 */
@Service
public class GoodsService {
    @Autowired
    private CutGoodsMapper cutGoodsMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private SearchMapper searchMapper;
    @Autowired
    private CutSearchMapper cutSearchMapper;

    /**
     * 查询商家上传的商品
     *
     * @param saleId
     * @return
     */
    public List<CutGoods> getGoodsBySaleId(long saleId) {
        return cutGoodsMapper.selectBySaleId(saleId);
    }

    /**
     * 获取商家可用的商品（可用：上架+数量>0）
     *
     * @return
     */
    public List<CutGoods> getEnableGoodsBySaleId(long saleId) {
        return cutGoodsMapper.selectEnableBySaleId(saleId);
    }


    /**
     * 新增商品
     *
     * @param goods
     */
    @Transactional
    public void add(Goods goods) {
        goods.setCreateTime(System.currentTimeMillis());
        if (goodsMapper.insertSelective(goods) == 0) {
            throw new RuntimeException("插入失败！");
        }
        Search search = new Search();
        search.setDid(goods.getId());
        search.setType(SearchTypeEnum.GOODS.getValue());
        search.setName(ParticipleUtil.participle(goods.getName()));
        if (searchMapper.insert(search) == 0) {
            throw new RuntimeException("插入失败！");
        }
    }

    /**
     * 获取商品详情
     *
     * @param goodsId
     * @return
     */
    public CutGoods getGoodsById(long goodsId) {
        return cutGoodsMapper.selectByPrimaryKey(goodsId);
    }


    /**
     * 更新
     *
     * @param goods
     */
    @Transactional
    public void update(Goods goods) {
        if (cutGoodsMapper.updateByPrimaryKeySelective(goods) == 0) {
            throw new RuntimeException("更新失败！");
        }
        if(goods.getName()!=null){
            Search search = new Search();
            search.setDid(goods.getId());
            search.setType(SearchTypeEnum.GOODS.getValue());
            search.setName(ParticipleUtil.participle(goods.getName()));
            if (cutSearchMapper.updateByPrimaryKeySelective(search) == 0) {
                throw new RuntimeException("更新失败！");
            }
        }
    }

    /**
     * 更新封面
     *
     * @param id
     * @param coverImageKey
     */
    public void updateCoverImage(long id, String coverImageKey) {
        Goods goods = new Goods();
        goods.setId(id);
        goods.setImageKey(coverImageKey);
        this.update(goods);
    }

    /**
     * 增加库存数
     *
     * @param id
     * @param number
     */
    public void addNumber(long id, long number) {
        if (cutGoodsMapper.addNumber(id, number) == 0) {
            throw new RuntimeException("更新失败！");
        }
    }

    /**
     * 减少库存数
     *
     * @param id
     * @param number
     */
    public void reduceNumber(long id, long number) {
        if (cutGoodsMapper.reduceNumber(id, number) == 0) {
            throw new RuntimeException("更新失败！");
        }
    }

    /**
     * 有该分类下的商品
     *
     * @param categoryId
     */
    public boolean hasCategoryGoods(long categoryId) {
        return cutGoodsMapper.selectCountByCategoryId(categoryId) > 0;
    }
}
